package com.google.gwt.sample.stockwatcher.client;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class HistoricoStockPrice {
	private Map<String,List<StockPrice>> historico = new HashMap<String,List<StockPrice>>();
	private static final int MAX_HISTORY = 49;
	
	/**
	 * @param sp StockPrice que añadimos al historico
	 */
	public void addStockPrice(StockPrice sp){
		String id = sp.getSymbol();
		
		List<StockPrice> lsp = historico.get(id);
		if (lsp==null){
			lsp = new LinkedList<StockPrice>();
			historico.put(id, lsp);
		}
		
		if(lsp.size()>MAX_HISTORY){
			lsp.remove(0);
		}
		
		lsp.add(sp);
		
	}
	
	/**
	 * @param sp El objeto StockPrice 
	 * @return Devuelve una lista en modo solo lectura
	 * con el historico del objeto StockPrice sp
	 */
	public List<StockPrice> getHistory(StockPrice sp){
		
		List<StockPrice> lsp = historico.get(sp.getSymbol());
		
		if (lsp != null){
			return Collections.unmodifiableList(lsp);
		}
		
		return null;
	}
	
	/**
	 * Limpia el historico
	 */
	public void removeHistory(){
		historico.clear();		
	}
	
	/**
	 * @param sp Elimina del historico el StockPrice que recibe
	 */
	public void removeStockPrice(String sp){
		historico.remove(sp);
	}
	
	
	
}
